<?php

abstract class Page
{
	protected $URL;
	protected $Authenticator;
	protected $Database;
	protected $StyleSheets = array();

	protected $SessionID;
	protected $User;
	protected $UserID;
	protected $Title = "SNAP";
	protected $showMenu = true;

	protected $userGroup = 0;
	
	protected $Action = "";
	protected $SubAction = "";
	
	public abstract function getHTML();

	public function __construct()
	{
		if(isset($_POST['Action']))
		{
			$this->Action = $_POST['Action'];
		}
		if(isset($_POST['SubAction']))
		{
			$this->SubAction = $_POST['SubAction'];
		}
	}

	public function setUserGroup($userGroup)
	{
		$this->userGroup = $userGroup;
	}
	
	protected function getUserGroup()
	{
		return $this->userGroup;
	}

	
	
	protected function getHeaderHTML()
	{
		$html = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
		$html = "<html xmlns=\"http://www.w3.org/1999/xhtml\" dir=\"ltr\" xml:lang=\"en_GB\" lang=\"en_GB\">\n";
		$html .= "<head>\n";
		$html .= "<title>".$this->Title."</title>\n";
		if (count($this->StyleSheets)>0)
		{
			foreach($this->StyleSheets as $stylesheet)
			{
				$html .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"".$stylesheet."\" />\n";
			}
		}
		else
		{
			$stylesheet = "http://".$_SERVER['SERVER_NAME']."/snap/Styles/snap.css";
			$html .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"".$stylesheet."\" />\n";
		}
		return $html;
	}

	protected function getBodyTopHTML()
	{
		$html = "</head>\n";
		$html .= "<body>\n";
		$POSTVariables = new PostVariables();
		$html .= $POSTVariables->getHTML();
		$html .= "<form id=\"PageForm\" action=\"index.php\" method = \"POST\">\n";
		$html .= "<h1>You are logged on - welcome " .$this->getUser()."</h1>\n";
		
		$showallmenu = false;
		if(($this->showMenu)&&($this->getUserGroup()!="Consult"))
		{
			$showallmenu=true;
		}
		$aMenu = new MenuSNAP($showallmenu);
		$html.= "<table style=\"margin:auto;\">\n";
		$html .= $aMenu->getHTML();
		$html .= "\n</table>\n<br>\n";


		
		return $html;
	}

	protected function getBodyBottomHTML()
	{
		$html = "</form>\n";
		$html .= "</body>\n";
		$html .= "</html>\n";
		return $html;
	}

	protected function getHiddenVariables()
	{
		$SessionID = new Hidden();
		$SessionID->setName("SessionID");
		$SessionID->setValue($this->SessionID);
		$html = $SessionID->gethtml();

		$User = new Hidden();
		$User->setName("User");
		$User->setValue($this->User);
		$html .= $User->gethtml();

		$UserID = new Hidden();
		$UserID->setName("UserID");
		$UserID->setValue($this->UserID);
		$html .= $UserID->gethtml();

		$Action = new Hidden();
		$Action->setName("Action");
		$Action->setValue($this->Action);
		$html .= $Action->gethtml();

/*		if (isset ($_POST['CallingPage']))
		{
			$CallingPage = new Hidden();
			$CallingPage->setName("CallingPage");
			$CallingPage->setValue($_POST['CallingPage']);
			$html .= $CallingPage->gethtml();
		}
*/
		if (isset ($_POST['View']))
		{
			$View = new Hidden();
			$View->setName("View");
			$View->setValue($_POST['View']);
			$html .= $View->gethtml();
		}

		return $html;
	}

	public function addStyleSheet($styleSheet)
	{
		if($styleSheet<>'')
			array_push($this->StyleSheets,$styleSheet);
	}

	public function setAuthenticator($anAuthenticator)
	{
		$this->Authenticator = $anAuthenticator;
	}

	public function getAuthenticator()
	{
		return $this->Authenticator;
	}
	
	public function setDatabase($aDatabase)
	{
		$this->Database = $aDatabase;
	}

	public function getDatabase()
	{
		return $this->Database;
	}
	
	public function setURL($aURL)
	{
		$this->URL = $aURL;
	}

	public function getUserID()
	{
		return $this->UserID;
	}

	public function getUser()
	{
		return $this->User;
	}

	public function isAuthenticated()
	{
		try
		{
			if(isset($_POST['SessionID']))
			{
//				echo $_POST['SessionID'].$_POST['User'];
				if($this->Authenticator->isLoggedOn($_POST['SessionID'],$_POST['User']))
				{
					$this->User = $_POST['User'];
					$this->SessionID = $_POST['SessionID'];
					$this->UserID = $this->Authenticator->getUserID();
					return true;
				}
				else
				{
					$this->Authenticator->displayLogOn();
					return false;
				}
			}
			if(isset($_POST['logonUsername']))
			{
//				echo $_POST['logonUsername'];
//				echo $_POST['logonPassword'];
				if ($this->Authenticator->tryLogOn(
//					$_POST['group'],
					"",
					$_POST['logonUsername'],
					$_POST['logonPassword']
					))
				{
					$this->User = $this->Authenticator->getUser();
					$this->UserID = $this->Authenticator->getUserID();
					$this->SessionID = $this->Authenticator->getSessionID();
//					echo "sessionID = ".$this->Authenticator->getSessionID();
					return true;
				}
				else
				{
					$this->Authenticator->displayLogOn();
					return false;
				}
			}
		}
		catch(Exception $error){} // 
		$this->Authenticator->displayLogOn();
		return false;
	}
}

?>